// This is part of libpooky, a JavaScript library that enhances
// PookyTouch or PookyTooth functionality.
// Copyright (C) 2009  PookyTeam <http://pooky.sourceforge.net/>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>


// depends on jquery-1.3.2.js
jQuery.fn.enableTouch = function(settings) {
    return this.each(function() {

        settings = jQuery.extend({
            move: true,
            rotate: true,
            scale: true,
            stopPropagation: true
        }, settings);
        
        var offset = $(this).offset();
        
        $(this).css({position: "absolute" , left: offset.left, top: offset.top });

        $(this).context.addEventListener("touchstart", function(event) {
            var obj = event.currentTarget;
            var rect = obj.getBoundingClientRect();
            for (var touch in event.changedTouches) {
                obj.dx = touch.clientX - rect.left;
                obj.dy = touch.clientY - rect.top;
            }
            if (settings.stopPropagation) event.stopPropagation();
        }, false);

        if (settings.move) {
            $(this).context.addEventListener("touchmove", function(event) {
                var obj = event.currentTarget;
                for (var touch in event.changedTouches) {
                    obj.style.left = (touch.clientX - obj.dx) + "px";
                    obj.style.top = (touch.clientY - obj.dy) + "px";
                }
                if (settings.stopPropagation) event.stopPropagation();
            }, false);
        }

        $(this).context.addEventListener("touchend", function(event) {
            var obj = event.currentTarget;
            obj.dx = 0;
            obj.dy = 0;
            if (settings.stopPropagation) event.stopPropagation();
        }, false);

        $(this).context.addEventListener("gesturechange", function(event) {
            if (settings.rotate && settings.scale) {
                $(event.currentTarget).css('-moz-transform', 'rotate(' + event.rotation + 'deg) scale(' + event.scale + ')');
            } else if (settings.rotate) {
                $(event.currentTarget).css('-moz-transform', 'rotate(' + event.rotation + 'deg)');
            } else if (settings.scale) {
                $(event.currentTarget).css('-moz-transform', 'scale(' + event.scale + ')');
            }
            if (settings.stopPropagation) event.stopPropagation();
        }, false);

        return this;

    });

}


jQuery.fn.addLongTouchListener = function(listener, settings) {
    return this.each(function() {

        settings = jQuery.extend({
            duration: 1000
        }, settings);
        
        var timer = null;

        $(this).context.addEventListener("touchstart", function(event) {
			timer = setTimeout(function(){listener(event)}, settings.duration)
        }, false);

        $(this).context.addEventListener("touchmove", function(event) {
      		clearTimeout(timer);
        }, false);
      
        $(this).context.addEventListener("touchend", function(event) {
      		clearTimeout(timer);
        }, false);

        $(this).context.addEventListener("touchcancel", function(event) {
        	clearTimeout(timer);
        }, false);

        return this;

    });

}


jQuery.visualizeTouches = function() {

    
	$("body").context.addEventListener("touchstart", function(touch) {
		var deb = $("<div id='touchid_" + touch.identifier + "'><img src='http://pooky.sourceforge.net/jpooky/images/cross-red.png'/><span id='touchinfo_" + touch.identifier + "'>(s" + touch.screenX + ",s" + touch.screenY + ") [c" + touch.clientX + ",c" + touch.clientY + "]</span></div>").css({position:"absolute", zIndex:10, left:touch.clientX, top:touch.clientY, color:"red"}).appendTo($("body"));
	},true);

	$("body").context.addEventListener("touchend", function(touch) {
		$("#touchid_"  + touch.identifier).remove();
	},true);
	
	$("body").context.addEventListener("touchcancel", function(touch) {
		$("#touchid_"  + touch.identifier).remove();
	},true);
	
	$("body").context.addEventListener("touchmove", function(touch) {
	    $("#touchinfo_"  + touch.identifier).text("(s" + touch.screenX + ",s" + touch.screenY + ") [c" + touch.clientX + ",c" + touch.clientY + "]");
		$("#touchid_"  + touch.identifier).css({left: touch.clientX, top:touch.clientY});
	},true);

};


